package com.lottery.service.pc28.wealthService.impl;

import com.alibaba.fastjson.JSON;
import com.lottery.dao.pc28.accoutDao.UserDao;
import com.lottery.dao.pc28.wealthDao.AccountDao;
import com.lottery.dao.pc28.wealthDao.BankcardDao;
import com.lottery.entity.Account;
import com.lottery.entity.Bankcard;
import com.lottery.entity.User;
import com.lottery.service.pc28.JedisClient;
import com.lottery.service.pc28.wealthService.BankCardService;
import com.lottery.service.pc28.wealthService.impl.config.BankcardConfig;
import com.lottery.service.pc28.wealthService.impl.uitil.BankUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class BankCardServiceImpl implements BankCardService {
	@Resource
	private BankcardDao bankcarddao;

	@Resource
	private AccountDao accountDao;

	@Resource
	private UserDao userDao;

	@Resource
	private JedisClient jedisClient;

	/**
	 * 判断有没有实名认证
	 * 
	 * @param userId
	 *            用户id
	 */

	public String Certification(int userId) {
		Map<String, Object> maxMap =new HashMap<>(); 
		try {
			User user = userDao.selectId(userId);

			String realName = user.getRealName(); // 用户真实姓名
			String phoneNum = user.getPhoneNum(); // 用户手机号
			if (realName != null && phoneNum != null) {
				maxMap.put("result", "0");
				maxMap.put("msg", "200");
			} else {
				maxMap.put("result", "1");
				maxMap.put("msg", "400");
			}
		} catch (Exception e) {
			e.printStackTrace();
			maxMap.put("result", "2");
			maxMap.put("msg", "400");
		}
		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 根据帐户ID查询银行卡
	 * 
	 * @param user_id
	 *            用户id
	 */
	public String selectBankCardByAccount_id(int userId) {
		Map<String, Object> maxMap =new HashMap<>(); 
		List<BankcardConfig> list = new ArrayList<BankcardConfig>();
		try {
			Account account = accountDao.selectAccountByUserId(userId);
			List<Bankcard> bankcardlist = bankcarddao.selectBankcardByAccountId(account.getId());
			if (bankcardlist != null) {
				for (Bankcard bankcard : bankcardlist) {
					String bankname = bankcard.getBankcardName();// 银行名称
					String bankcardNumber = bankcard.getBankcardNumber();// 银行卡号
					String bankcardType = bankcard.getBankcardType();// 银行卡类型
					list.add(new BankcardConfig(bankname, bankcardNumber, bankcardType));
				}
				maxMap.put("array", list);
				maxMap.put("result", "0");
				maxMap.put("msg", "200");

			} else {
				maxMap.put("result", "1");
				maxMap.put("msg", "200");
			}
		} catch (Exception e) {
			maxMap.put("result", "1");
			maxMap.put("msg", "400" + e.toString());
		}
		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 根据用户id查询用户真实姓名和电话号码
	 * 
	 * @param user_id
	 *            用户id
	 */

	public String getUsername(int userId) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			// username=调用连康用户表接口出入user_id得到real_name
			String realName = userDao.selectId(userId).getRealName();
			String phoneNum = userDao.selectId(userId).getPhoneNum();
			map.put("real_name", realName);
			map.put("phone_num", phoneNum);
			map.put("result", "0");
			map.put("msg", "200");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "1");
			map.put("msg", "400");
		}
		return JSON.toJSONString(map, true);
	}

	/**
	 * 发送短信验证码
	 * 
	 * @param int
	 *            user_id
	 * @return
	 */

	public String SendSMS(int userId) {
		Map<String, Object> maxMap =new HashMap<>();
		try {
			// 调用发送短信api
			String phoneNum = userDao.selectId(userId).getPhoneNum();
			HashMap<String, String> map = JavaSmsApi.getMessageStatus(phoneNum);
			String code = map.get("code");
			jedisClient.set("loginsystem" + phoneNum, code);
			maxMap.put("result", "0");
			maxMap.put("msg", "200");
		} catch (Exception e) {
			e.printStackTrace();
			maxMap.put("result", "1");
			maxMap.put("msg", "异常");
		}
		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 验证短信是否通过 添加银行卡到数据库
	 */
	public String insertBankcard(int userId, String code, String bankcard_number,
			String bankcardusername) {
		Map<String, Object> maxMap =new HashMap<>(); 
		try {
			if (bankcard_number == null && bankcard_number.equals("")) {
				maxMap.put("result", "1");
				maxMap.put("msg", "银行卡号有误");
				return JSON.toJSONString(maxMap, true);
			}
			String phoneNum = userDao.selectId(userId).getPhoneNum();// 手机号
			String userCode = jedisClient.get("loginsystem" + phoneNum);
			if (userCode.equals(code)) {// 进行验证，通过后插入操作
				Map<String, String> map = getBankCardname(bankcard_number);
				if (map.isEmpty()) {
					maxMap.put("result", "1");
					maxMap.put("msg", "银行卡号有误");
					return JSON.toJSONString(maxMap, true);
				}
				String bankcardname = map.get("bankcardname");// 银行名称
				if (bankcardname == null && bankcardname.equals("")) {
					maxMap.put("result", "1");
					maxMap.put("msg", "银行卡号有误");
					return JSON.toJSONString(maxMap, true);
				}
				String bankcard_type = map.get("bankcard_type");// 银行类型
				if (bankcard_type == null && bankcard_type.equals("")) {
					maxMap.put("result", "1");
					maxMap.put("msg", "银行卡号有误");
					return JSON.toJSONString(maxMap, true);
				}
				Bankcard bankcard = new Bankcard();
				int account_id = accountDao.selectAccountByUserId(userId).getId();
				List<Bankcard> list = bankcarddao.selectBankcardByAccountId(account_id);
				for (Bankcard bankcard2 : list) {
					String bankcardNumber = bankcard2.getBankcardNumber();
					if (bankcardNumber.equals(bankcard_number)) {
						maxMap.put("result", "2");
						maxMap.put("msg", "此卡已经绑定");
						return JSON.toJSONString(maxMap, true);
					}
				}
				bankcard.setAccountId(account_id);
				bankcard.setBankcardNumber(bankcard_number);
				bankcard.setBankcardType(bankcard_type);
				bankcard.setBankcardName(bankcardname);
				bankcard.setBindDate(new Date());
				bankcard.setBankcardusername(bankcardusername);
				bankcarddao.insertBankcard(bankcard);
				maxMap.put("array", bankcard);
				maxMap.put("result", "0");
				maxMap.put("msg", "200");
				return JSON.toJSONString(maxMap, true);
			} else {
				maxMap.put("result", "3");
				maxMap.put("msg", "您输入的验证码有误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			maxMap.put("result", "4");
			maxMap.put("msg", "添加失败");
			return JSON.toJSONString(maxMap, true);
		}

		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 根据银行卡号获取银行类型和名称
	 * 
	 * @param bankcard_number
	 *            银行卡号
	 */
	public Map<String, String> getBankCardname(String bankcard_number) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			String bank = BankUtil.getNameOfBank(bankcard_number);
			String[] split = bank.split("·");
			String bankcardname = split[0];
			String bankcard_type = split[1];
			map.put("bankcardname", bankcardname);
			map.put("bankcard_type", bankcard_type);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 删除银行卡
	 * 
	 * @param bankcard_number
	 *            卡号
	 * @param accountId
	 *            用户id
	 * @return
	 */
	@Override
	public String deleteBankcard(int userId, String bankcardNumber) {
		Map<String, Object> maxMap =new HashMap<>(); 
		int accountId = accountDao.selectAccountByUserId(userId).getId();
		List<Bankcard> list = bankcarddao.selectBankcardByAccountId(accountId);
		int id = 0;
		for (Bankcard bankcard1 : list) {
			String number = bankcard1.getBankcardNumber();
			if (number.equals(bankcardNumber)) {
				id = bankcard1.getId();
			}
		}
		Bankcard bankcard = new Bankcard();
		bankcard.setId(id);
		bankcard.setAccountId(accountId);
		bankcard.setBankcardDelete(1);
		try {
			bankcarddao.updateBankcardById(bankcard);
			maxMap.put("result", "0");
			maxMap.put("msg", "成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			maxMap.put("result", "1");
			maxMap.put("msg",e.toString());
		}
		return JSON.toJSONString(maxMap, true);
	}

}
